// pages/zptj/zptj.js
const app = getApp()
const db = wx.cloud.database()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goods: '',
    worktime: '',
    location: '',
    brief:'',
    method:'',
    pics:'',
    imgsrc:'',
    new: true,
    // oldgoods:''
  },
  //获取品名
  goodinput(e){
    this.data.goods = e.detail.value
  },
  //获取营业时间
  timeinput(e){
    this.data.worktime = e.detail.value
  },
  //获取地点
  locationBlur(e){
    this.data.location = e.detail.value
  },
  //获取简介
  briefBlur(e){
    this.data.brief = e.detail.value
  },
  //获取使用方法
  methodBlur(e){
    this.data.method = e.detail.value
  },
  //获取商户图片
  uploadimg(){
    var that = this
    wx.showLoading({
      title: '',
    })
    // 让用户选择一张图片
    wx.chooseMedia({
      count: 1,
      mediaType: ['image',],
      sourceType: ['album', ],
      success: chooseResult => {
        wx.cloud.uploadFile({
          cloudPath: Date.now()+'.png',
          // 指定要上传的文件的小程序临时文件路径
          filePath: chooseResult.tempFiles[0].tempFilePath,
        }).then(res => {
          console.log('上传成功', res)
          that.data.pics = res.fileID
          wx.cloud.callFunction({
            name:'getimghttp',
            data:{
              img:res.fileID
            }
          })
          .then(res=>{
            console.log('imgsrc',res)
            that.data.imgsrc = res.result[0].tempFileURL
            wx.hideLoading()
          })
          // this.setData({
          //   pics: res.fileID
          // })
          // wx.hideLoading()
        }).catch((e) => {
          console.log(e)
          wx.hideLoading()
        })
      },
    })
  },

  // 添加或者修改gifts数据
  addorupdate() {
    var that = this
    console.log(that.data.new)
    if (that.data.new){
      if (that.data.goods == ''){
        wx.showModal({
          title: '温馨提醒',
          content: '请输入赠品名称！',
          showCancel: false
        })
      }
      else if (that.data.worktime == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入营业时间！',
          showCancel: false
        })
      }
      else if (that.data.location == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入营业地点',
          showCancel: false
        })
      }
      else if (that.data.brief == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入赠品简介',
          showCancel: false
        })
      }
      else if (that.data.method == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请输入使用方法',
          showCancel: false
        })
      }
      else if (that.data.imgsrc == '') {
        wx.showModal({
          title: '温馨提醒',
          content: '请上传商户照片',
          showCancel: false
        })
      }
      else{
        db.collection('gifts')
        .where({
          goods:that.data.goods
        })
        .get()
        .then(res=>{
          console.log(res.data.length)
          if(res.data.length == 0){
            db.collection('gifts').add({
              data: {
                block:app.globalData.block,
                goods:that.data.goods,
                worktime:that.data.worktime,
                location:that.data.location,
                brief:that.data.brief,
                method:that.data.method,
                pics:that.data.pics,
                imgsrc:that.data.imgsrc
              }
            })
            .then(res => {
              wx.showModal({
                title: '恭喜',
                content: '赠品添加成功！',
                showCancel: false,
                complete() {
                  // 跳转到zpba页面
                  wx.redirectTo({
                    url: '/pages/zpba/zpba'
                  })
                }
              })
            })
          }else{
            wx.showModal({
              title: '提醒',
              content: '赠品库已有该赠品',
              showCancel: false,
            })
          }
        })

      }
    }
    else{
      db.collection('gifts').where({
        goods:that.data.goods
      }).get({
        success:function(res){
          if(res.data.length == 1 ){
            console.log(res.data[0]._id)
            wx.cloud.callFunction({
              name: 'zpgl_update',
              data: {
                DBname:'gifts',
                goodsid: res.data[0]._id,
                worktime:that.data.worktime,
                location:that.data.location,
                brief:that.data.brief,
                method:that.data.method,
                pics:that.data.pics,
                imgsrc:that.data.imgsrc
              }
            }).then(res => {
              console.log('信息修改成功',res)
              wx.showModal({
                title: '恭喜',
                content: '信息修改成功！',
                showCancel: false,
                complete() {
                  // 跳转到zpba页面
                  wx.redirectTo({
                    url: '/pages/zpba/zpba'
                  })
                }
              })
            }).catch(err => {
              console.log(err)
            })
          }else{
            wx.showModal({
              title: '温馨提醒',
              content: '赠品备案登记有问题，请联系客服',
              showCancel: false
            })            
          }
        },
        fail:function(){
          console.log("搜素赠品失败");
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    if (options.goods) {
      that.setData({
        goods:options.goods
      })
      that.setData({
        new:false
      })
      db.collection('gifts')
      .where({
        goods:options.goods
      })
      .get({
        success:function(res){
          // that.data.oldgoods = res.data[0].goods
          that.setData({
            // goods:res.data[0].goods,
            worktime:res.data[0].worktime,
            location:res.data[0].location,
            brief:res.data[0].brief,
            method:res.data[0].method,
            imgsrc:res.data[0].imgsrc
          })
        },
        fail:function(err){
          console.log('err',err)
        },
        complete:function(e){
          console.log("complete",e)
          if (e.data.length == 0){
            // that.data.new = true
            that.setData({
              new:true
            })
          }
        }
      })
    }else{
      console.log('这次是添加数据')
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})